package wqh.controller.json;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.WaterChannelAlarm;
import wqh.domain.WaterChannelAlarmLog;
import wqh.pojo.MapRet;
import wqh.service.WaterChannelAlarmLogService;
import wqh.service.WaterChannelAlarmService;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by wqh on 2017/1/14.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/json/water_device/channel/alarm")
public class WaterChannelAlarmJSON {

    @Autowired
    private WaterChannelAlarmLogService waterChannelAlarmLogService;

    @Autowired
    private WaterChannelAlarmService waterChannelAlarmService;

    @RequestMapping(value = "/confirm",method = RequestMethod.POST)
    public Object confirm(@RequestBody WaterChannelAlarmLog w){
        try {
            if(w.getConfirm_time() == null ||
                    w.getStatus() == null ||
                    w.getDevice_id() == null ||
                    w.getChannel_id() == null ||
                    w.getUser_name() == null)
                return MapRet.ERROR();
            List<WaterChannelAlarm> list = waterChannelAlarmService.findByDeviceIdAndChannelNumber(w.getDevice_id(),w.getChannel_id());
            if(list == null)  return MapRet.ERROR();
            for(WaterChannelAlarm wa : list){
                waterChannelAlarmLogService.save(new WaterChannelAlarmLog(wa.getAlarm_time(),
                        w.getConfirm_time(),
                        w.getStatus(),
                        w.getDevice_id(),
                        w.getChannel_id(),
                        w.getUser_name(),
                        wa.getValue()));
            }
            waterChannelAlarmService.deleteByDeviceIdAndChannelNumber(w.getDevice_id(),w.getChannel_id());
            return MapRet.SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.ERROR();
        }
    }

    @RequestMapping(value = "/selectLog",method = RequestMethod.POST)
    public Object selectLog(@RequestBody Map<String,Integer> map){
        try {
            int start = map.get("start");
            int end = map.get("end");
            if(start > end || start == 0 || end == 0) return MapRet.DATA(null);
            long confirmed = waterChannelAlarmLogService.countByTimeRange(start,end);
            long unconfirmed = waterChannelAlarmService.countByTimeRange(start,end);
            Map<String, Object> ret = new HashMap<>();
            ret.put("confirmed",confirmed);
            ret.put("unconfirmed",unconfirmed);
            return MapRet.DATA(ret);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.DATA(null);
        }
    }

    @RequestMapping(value = "/yearLog",method = RequestMethod.POST)
    public Object yearLog(@RequestBody Map<String,Integer> map){
        try {
            int year = map.get("year");
            if(year < 1970) return MapRet.DATA(null);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            List<Map<String,Object>> list = new ArrayList<>();
            for(int i = 0; i < 12 ; i++){
                Calendar calStart = Calendar.getInstance();
                calStart.set(Calendar.YEAR,year);
                calStart.set(Calendar.MONTH,i);
                calStart.set(Calendar.DAY_OF_MONTH,1);
//                System.out.println("Start##############");
//                System.out.println(format.format(calStart.getTime()));
//                System.out.println(format.parse(format.format(calStart.getTime())).getTime() / 1000);
//
//                System.out.println("end##############");
//                System.out.println(format.format(calStart.getTime()));
//                calStart.set(Calendar.DAY_OF_MONTH,calStart.getActualMaximum(Calendar.DAY_OF_MONTH));
//                System.out.println(format.parse(format.format(calStart.getTime())).getTime() / 1000);
                int start = (int) (format.parse(format.format(calStart.getTime())).getTime() / 1000);
                calStart.set(Calendar.DAY_OF_MONTH,calStart.getActualMaximum(Calendar.DAY_OF_MONTH));
                int end = (int) (format.parse(format.format(calStart.getTime())).getTime() / 1000);
                Map<String, Object> data = new HashMap<>();
                data.put("month",i+1);
                data.put("confirmed",waterChannelAlarmLogService.countByTimeRange(start,end));
                data.put("unconfirmed",waterChannelAlarmService.countByTimeRange(start,end));
                list.add(data);
            }

            return MapRet.DATA(list);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.DATA(null);
        }
    }

    @RequestMapping(value = "/monthLog",method = RequestMethod.POST)
    public Object monthLog(@RequestBody Map<String,Integer> map){
        try {
            int year = map.get("year");
            if(year < 1970) return MapRet.DATA(null);
            int month = map.get("month");
            if(month < 1 || month > 12)return MapRet.DATA(null);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<Map<String,Object>> list = new ArrayList<>();
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR,year);
            cal.set(Calendar.MONTH,month - 1);
            int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            for(int i = 0; i < days; i++){
                String timeStartStr = year + "-" + month + "-" + (i+1) + " 00:00:00";
                String timeEndStr = year + "-" + month + "-" + (i+1) + " 23:59:59";
                int start = (int) (format.parse(timeStartStr).getTime() / 1000);
                int end = (int) (format.parse(timeEndStr).getTime() / 1000);
                Map<String, Object> data = new HashMap<>();
                data.put("day",i+1);
                data.put("confirmed",waterChannelAlarmLogService.countByTimeRange(start,end));
                data.put("unconfirmed",waterChannelAlarmService.countByTimeRange(start,end));
                list.add(data);
            }
            return MapRet.DATA(list);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.DATA(null);
        }
    }

}
